package com.becca.cake.controller.system;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.becca.cake.core.common.BaseAction;
import com.becca.cake.model.system.DataDictionaryDO;
import com.becca.cake.service.system.IDataDictionaryService;
import com.becca.cake.util.JsonUtil;
import com.becca.cake.util.StringUtil;

/**
 * 数据字典管理
 * @author hejinguo
 * @version $Id: DataDictionaryAction.java, v 0.1 2013-12-20 上午01:21:55
 */
@Controller
@RequestMapping("/systemDataDictionary")
public class DataDictionaryAction extends BaseAction {
    private final static Log       logger = LogFactory.getLog(DataDictionaryAction.class);
    @Autowired
    private IDataDictionaryService iDataDictionaryService;

    /**
     * 加载数据字典
     * @author hejinguo
     * @return
     */
    @RequestMapping(value = "/selectDictionaryTreeMenu", method = RequestMethod.POST)
    public @ResponseBody
    String selectDictionaryTreeMenu() {
        try {
            return this.iDataDictionaryService.selectDictionaryTreeMenu();
        } catch (Exception e) {
            logger.error("加载数据字典树失败!", e);
            return JsonUtil.returnJsonUnSuccess("加载数据字典树失败!");
        }
    }

    /**
     * 根据ID查询数据字典信息
     * @author hejinguo
     * @param id
     * @return
     */
    @RequestMapping(value = "/selectDataDictionaryMsgById", method = RequestMethod.POST)
    public @ResponseBody
    String selectDataDictionaryMsgById(@RequestParam(value = "id", required = false) String id) {
        if (StringUtil.isNotEmpty(id)) {
            try {
                DataDictionaryDO dataDictionaryDO = this.iDataDictionaryService
                    .selectDataDictionaryMsgById(id);
                return JsonUtil.writeObjectFormJSON(dataDictionaryDO);
            } catch (Exception e) {
                logger.error("查询数据字典信息失败!", e);
                return JsonUtil.returnJsonUnSuccess("查询数据字典信息失败!");
            }
        } else {
            logger.error("根据ID查询数据字典信息ID为空!");
            return JsonUtil.returnJsonUnSuccess("根据ID查询数据字典信息ID为空!");
        }
    }

    /**
     * 添加数据字典信息
     * @author hejinguo
     * @param functionDO
     * @return
     */
    @RequestMapping(value = "/insetDataDictionary", method = RequestMethod.POST)
    public @ResponseBody
    String insetDataDictionary(@ModelAttribute DataDictionaryDO dataDictionaryDO) {
        if (dataDictionaryDO != null) {
            dataDictionaryDO.setCreatedMan(this.getUserSeesion().getTrueName());
            try {
                this.iDataDictionaryService.insetDataDictionary(dataDictionaryDO);
                return JsonUtil.returnJsonSuccess("添加成功!");
            } catch (Exception e) {
                logger.error("添加失败!", e);
                return JsonUtil.returnJsonUnSuccess("添加失败!");
            }
        } else {
            logger.error("添加数据字典信息时信息为空!");
            return JsonUtil.returnJsonUnSuccess("添加数据字典信息时信息为空!");
        }
    }

    /**
     * 修改数据字典信息
     * @author hejinguo
     * @param functionDO
     * @return
     */
    @RequestMapping(value = "/updateDataDictionaryById", method = RequestMethod.POST)
    public @ResponseBody
    String updateDataDictionaryById(@ModelAttribute DataDictionaryDO dataDictionaryDO) {
        if (dataDictionaryDO != null) {
            dataDictionaryDO.setUpdatedMan(this.getUserSeesion().getTrueName());
            try {
                this.iDataDictionaryService.updateDataDictionaryById(dataDictionaryDO);
                return JsonUtil.returnJsonSuccess("修改成功!");
            } catch (Exception e) {
                logger.error("修改失败!", e);
                return JsonUtil.returnJsonUnSuccess("修改失败!");
            }
        } else {
            logger.error("修改数据字典信息时信息为空!");
            return JsonUtil.returnJsonUnSuccess("修改数据字典信息时信息为空!");
        }
    }

    /**
     * 数据字典中根据类型显示该类型下的全部数据 
     * @author hejinguo
     * @return
     */
    @RequestMapping(value = "/selectDataDictionaryById", method = RequestMethod.POST)
    public @ResponseBody
    String selectDataDictionaryById(@RequestParam(value = "dictionaryType") String dictionaryType,
                                    @RequestParam(value = "existall") String existall,
                                    @RequestParam(value = "useable") String useable) {
        try {
            List<DataDictionaryDO> list = this.iDataDictionaryService.selectDataDictionary(
                dictionaryType, useable);
            if (StringUtils.isNotEmpty(existall) && existall.equals("true")) {
                DataDictionaryDO au = new DataDictionaryDO();
                au.setKeyes("全部");
                list.add(au);
            }
            return JsonUtil.writeObjectNoPageJSON(list);
        } catch (Exception e) {
            logger.error("查询数据字典失败!", e);
            return JsonUtil.returnJsonUnSuccess("查询数据字典失败!");
        }
    }

}
